
#import "GetUserDetailsRequest.h"

@implementation GetUserDetailsRequest

//
// user id
//
@synthesize userDetails														= _userDetails;

//
// init
//
- (id)init
{
	//
	// call super
	//
	self																	= [super init];
	if(!self)
		return nil;
	
	//
	// create xml parser, user details
	//
	self.xmlParser															= [[[XmlParser alloc] init] autorelease];
	self.userDetails														= [[[UserDetails alloc] init] autorelease];
	
	return self;
}

//
// dealloc
//
- (void)dealloc
{
	self.userDetails														= nil;
	
	[super dealloc];
}

//
// setup request
//
- (void)setupRequest
{
	//
	// setup relpath
	//
	self.relPath															= [NSString stringWithFormat:@"userdetails.php?id=%d", self.userDetails.userId];
	
	//
	// call super
	//
	[super setupRequest];
}

//
// finished
//
- (void)connectionDidFinishLoading:(NSURLConnection*)connection
{
	if(self.response.statusCode == 200)
	{
		[self.xmlParser parseDocumentData:self.dataBuffer htmlMode:YES];
		NSArray* infoRows													= [self.xmlParser nodesFromPath:@"//td[@class='embedded']/table/tr" startNode:0];
		if(infoRows.count >= 13)
		{
			NSString* temp													= [self.xmlParser stringFromPath:@"./td[@class='rowhead nowrap']" startNode:(xmlNodePtr)[[infoRows objectAtIndex:1] pointerValue]];
			NSInteger startIndex											= [temp isEqualToString:@"Invited By"] ? 2 : 1;
			self.userDetails.userName										= [self.xmlParser stringFromPath:@"//td[@id='outer']/h1/span/b" startNode:0];
			self.userDetails.joinDate										= [self.xmlParser stringFromPath:@"./td[@class='rowfollow']/span/@title" startNode:(xmlNodePtr)[[infoRows objectAtIndex:startIndex + 0] pointerValue]];
			self.userDetails.lastDate										= [self.xmlParser stringFromPath:@"./td[@class='rowfollow']/span/@title" startNode:(xmlNodePtr)[[infoRows objectAtIndex:startIndex + 1] pointerValue]];
			if([DataCenter sharedDataCenter].userId == self.userDetails.userId)
			{
				self.userDetails.lastIp										= [self.xmlParser stringFromPath:@"./td[@class='rowfollow']" startNode:(xmlNodePtr)[[infoRows objectAtIndex:startIndex + 2] pointerValue]];
			}
			else
			{
				self.userDetails.lastIp										= @"---";
				startIndex													-= 1;
			}
			
			if([[self.xmlParser stringFromPath:@"./td[@class='rowhead nowrap']" startNode:(xmlNodePtr)[[infoRows objectAtIndex:startIndex + 3] pointerValue]] isEqualToString:@"BT Client"])
				self.userDetails.clientList									= [[self.xmlParser stringFromPath:@"./td[@class='rowfollow']" startNode:(xmlNodePtr)[[infoRows objectAtIndex:startIndex + 3] pointerValue]] componentsSeparatedByString:@" ; "];
			else
				startIndex													-= 1;
			
			temp															= [self.xmlParser stringFromPath:@"./td[@class='rowfollow']/table/tr[1]/td[1]/font" startNode:(xmlNodePtr)[[infoRows objectAtIndex:startIndex + 4] pointerValue]];
			if(!temp.length)
				temp														= @"---";
			self.userDetails.shareRatio										= NSLocalizedString(temp, nil);
			
			self.userDetails.uploadedSize									= [[self.xmlParser stringFromPath:@"./td[@class='rowfollow']/table/tr[2]/td[1]" startNode:(xmlNodePtr)[[infoRows objectAtIndex:startIndex + 4] pointerValue]] stringByReplacingOccurrencesOfString:@"Uploaded:" withString:@""];
			if(!self.userDetails.uploadedSize.length)
				self.userDetails.uploadedSize								= [[self.xmlParser stringFromPath:@"./td[@class='rowfollow']/table/tr[1]/td[1]" startNode:(xmlNodePtr)[[infoRows objectAtIndex:startIndex + 4] pointerValue]] stringByReplacingOccurrencesOfString:@"Uploaded:" withString:@""];
			
			self.userDetails.downloadedSize									= [[self.xmlParser stringFromPath:@"./td[@class='rowfollow']/table/tr[2]/td[2]" startNode:(xmlNodePtr)[[infoRows objectAtIndex:startIndex + 4] pointerValue]] stringByReplacingOccurrencesOfString:@"Downloaded:" withString:@""];
			if(!self.userDetails.downloadedSize.length)
				self.userDetails.downloadedSize								= [[self.xmlParser stringFromPath:@"./td[@class='rowfollow']/table/tr[1]/td[2]" startNode:(xmlNodePtr)[[infoRows objectAtIndex:startIndex + 4] pointerValue]] stringByReplacingOccurrencesOfString:@"Downloaded:" withString:@""];
			
			temp															= [self.xmlParser stringFromPath:@"./td[@class='rowfollow']/table/tr[1]/td[1]/font" startNode:(xmlNodePtr)[[infoRows objectAtIndex:startIndex + 5] pointerValue]];
			if(!temp.length)
				temp														= @"---";
			self.userDetails.timeRatio										= NSLocalizedString(temp, nil);
			
			temp															= [[self.xmlParser stringFromPath:@"./td[@class='rowfollow']/table/tr[2]/td[1]" startNode:(xmlNodePtr)[[infoRows objectAtIndex:startIndex + 5] pointerValue]] stringByReplacingOccurrencesOfString:@"Seeding Time:" withString:@""];
			if(!temp.length)
				temp														= [[self.xmlParser stringFromPath:@"./td[@class='rowfollow']/table/tr[1]/td[1]" startNode:(xmlNodePtr)[[infoRows objectAtIndex:startIndex + 5] pointerValue]] stringByReplacingOccurrencesOfString:@"Seeding Time:" withString:@""];
			self.userDetails.seedTime										= [NSString formatTime:[temp decodeTime]];
			
			temp															= [[self.xmlParser stringFromPath:@"./td[@class='rowfollow']/table/tr[2]/td[2]" startNode:(xmlNodePtr)[[infoRows objectAtIndex:startIndex + 5] pointerValue]] stringByReplacingOccurrencesOfString:@"Leeching Time:" withString:@""];
			if(!temp.length)
				temp														= [[self.xmlParser stringFromPath:@"./td[@class='rowfollow']/table/tr[1]/td[2]" startNode:(xmlNodePtr)[[infoRows objectAtIndex:startIndex + 5] pointerValue]] stringByReplacingOccurrencesOfString:@"Leeching Time:" withString:@""];
			self.userDetails.leechTime										= [NSString formatTime:[temp decodeTime]];
			
			if([[self.xmlParser stringFromPath:@"./td[@class='rowhead nowrap']" startNode:(xmlNodePtr)[[infoRows objectAtIndex:startIndex + 6] pointerValue]] isEqualToString:@"Network Bandwidth"])
				self.userDetails.bandwidth									= [self.xmlParser stringFromPath:@"./td[@class='rowfollow']" startNode:(xmlNodePtr)[[infoRows objectAtIndex:startIndex + 6] pointerValue]];
			else
				startIndex													-= 1;
			
			self.userDetails.gender											= [self.xmlParser stringFromPath:@"./td[@class='rowfollow']/img/@class" startNode:(xmlNodePtr)[[infoRows objectAtIndex:startIndex + 7] pointerValue]];
			if([[self.xmlParser stringFromPath:@"./td[@class='rowhead nowrap']" startNode:(xmlNodePtr)[[infoRows objectAtIndex:startIndex + 8] pointerValue]] isEqualToString:@"Avatar"])
				startIndex													+= 1;
			
			self.userDetails.userClass										= [self.xmlParser stringFromPath:@"./td[@class='rowfollow']/img/@title" startNode:(xmlNodePtr)[[infoRows objectAtIndex:startIndex + 8] pointerValue]];
			if([DataCenter sharedDataCenter].userId == self.userDetails.userId)
			{
				temp														= [self.xmlParser stringFromPath:@"./td[@class='rowfollow']" startNode:(xmlNodePtr)[[infoRows objectAtIndex:startIndex + 13] pointerValue]];
				self.userDetails.karma										= [[temp componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] objectAtIndex:0];
				self.userDetails.uCoin										= [self.xmlParser stringFromPath:@"./td[@class='rowfollow']" startNode:(xmlNodePtr)[[infoRows objectAtIndex:startIndex + 14] pointerValue]];
				self.userDetails.uploadKarma								= [self.xmlParser stringFromPath:@"./td[@class='rowfollow']" startNode:(xmlNodePtr)[[infoRows objectAtIndex:startIndex + 15] pointerValue]];
				self.userDetails.downloadKarma								= [self.xmlParser stringFromPath:@"./td[@class='rowfollow']" startNode:(xmlNodePtr)[[infoRows objectAtIndex:startIndex + 16] pointerValue]];
			}
			else
			{
				temp														= [self.xmlParser stringFromPath:@"./td[@class='rowfollow']" startNode:(xmlNodePtr)[[infoRows objectAtIndex:startIndex + 11] pointerValue]];
				self.userDetails.karma										= [[temp componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] objectAtIndex:0];
				self.userDetails.uCoin										= [self.xmlParser stringFromPath:@"./td[@class='rowfollow']" startNode:(xmlNodePtr)[[infoRows objectAtIndex:startIndex + 12] pointerValue]];
				self.userDetails.uploadKarma								= [self.xmlParser stringFromPath:@"./td[@class='rowfollow']" startNode:(xmlNodePtr)[[infoRows objectAtIndex:startIndex + 13] pointerValue]];
				self.userDetails.downloadKarma								= [self.xmlParser stringFromPath:@"./td[@class='rowfollow']" startNode:(xmlNodePtr)[[infoRows objectAtIndex:startIndex + 14] pointerValue]];
			}
			
			double up														= [self.userDetails.uploadKarma doubleValue];
			double down														= [self.userDetails.downloadKarma doubleValue];
			self.userDetails.realShareRatio									= down ? [NSString stringWithFormat:@"%.4g", -up / down] : NSLocalizedString(@"Inf.", nil);
		}
	}
	
	[super connectionDidFinishLoading:connection];
}

@end
